package com.microcore.service.asserts.condition;

import com.microcore.service.asserts.impl.Assertor;
import com.microcore.service.asserts.impl.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeiZhenYang
 * @date 2019.01.07
 */
public class CompareCondition {

    /**
     * 等于
     */
    public static int eq = 0;

    /**
     * 不等于
     */
    public static int neq = 1;

    /**
     * 大于
     */
    public static int gt = 2;

    /**
     * 小于
     */
    public static int lt = 3;

    /**
     * 大于等于
     */
    public static int gteq = 4;

    /**
     * 小于等于
     */
    public static int lteq = 5;

    /**
     * 包含
     */
    public static int contains = 6;

    /**
     * 不包含
     */
    public static int notContains = 7;

    /**
     * 为空
     */
    public static int isNull = 8;

    /**
     * 不为空
     */
    public static int isNotNull = 9;

    /**
     * 真
     */
    public static int true0 = 10;

    /**
     * 假
     */
    public static int false0 = 11;


    public static class Type {

        public static String Obj = "OBJECT";

        public static String Str = "STRING";

        public static String Num = "NUMBER";

        public static String Bool = "BOOLEAN";

    }

    /**
     * 条件关系分组
     */
    private static Map<String, List<Assertor>> group = new HashMap<>();

    static {
        List<Assertor> conditions;
        // 对象
        conditions = new ArrayList<>();
        conditions.add(new Assertor("为空", isNull, IsNullAssert.class));
        conditions.add(new Assertor("不为空", isNotNull, isNotNullAssert.class));
        group.put(Type.Obj, conditions);

        // 字符串
        conditions = new ArrayList<>();
        conditions.add(new Assertor("为空", isNull, IsNullAssert.class));
        conditions.add(new Assertor("不为空", isNotNull, isNotNullAssert.class));
        conditions.add(new Assertor("等于", eq, EqualAssert.class));
        conditions.add(new Assertor("不等于", neq, NotEqualAssert.class));
        conditions.add(new Assertor("包含", contains, ContainsAssert.class));
        conditions.add(new Assertor("不包含", notContains, NotContainsAssert.class));
        group.put(Type.Str, conditions);

        // 数值
        conditions = new ArrayList<>();
        conditions.add(new Assertor("为空", isNull, IsNullAssert.class));
        conditions.add(new Assertor("不为空", isNotNull, isNotNullAssert.class));
        conditions.add(new Assertor("等于", eq, EqualAssert.class));
        conditions.add(new Assertor("不等于", neq, NotEqualAssert.class));
        conditions.add(new Assertor("大于", gt, GreaterAssert.class));
        conditions.add(new Assertor("小于", lt, LessAssert.class));
        conditions.add(new Assertor("大于等于", gteq, GreaterEqualAssert.class));
        conditions.add(new Assertor("小于等于", lteq, LessEqualAssert.class));
        group.put(Type.Num, conditions);

        // 布尔
        conditions = new ArrayList<>();
        conditions.add(new Assertor("为空", isNull, IsNullAssert.class));
        conditions.add(new Assertor("不为空", isNotNull, isNotNullAssert.class));
        conditions.add(new Assertor("真", true0, TrueAssert.class));
        conditions.add(new Assertor("假", false0, FalseAssert.class));
        group.put(Type.Bool, conditions);
        // 日期（暂时不考虑）
    }

    /**
     * 获取条件集合
     *
     * @return
     */
    public static Map<String, List<Assertor>> getConditionGroup() {
        return group;
    }

    /**
     * 按组名称获取条件集合
     * +
     *
     * @return
     */
    public static List<Assertor> getConditionGroup(String groupName) {
        return group.get(groupName);
    }

    /**
     * 按组名称获取条件集合
     *
     * @return
     */
    public static Assertor getConditionByGroupAndIndex(String groupName, int index) {
        List<Assertor> assertors = group.get(groupName.toUpperCase());
        Assertor assertor = assertors.stream().filter((Assertor element) -> element.getIndex() == index).findFirst().orElse(null);
        if (assertor == null) {
            throw new NullPointerException("无效的Condition索引");
        }
        return assertor;
    }

}
